home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 October: Mac OS SDK / Dev.CD Oct 97 SDK2.toast / Development Kits (Disc 2) / ScriptX / Code Samples / autofind / source / ads.sx < prev    next >
Encoding:
Text File  |  1996-05-21  |  7.4 KB  |  236 lines  |  [TEXT/ttxt]

  1. -- <<<
  2. -- Filename:
  3. --     ads.sx
  4. --
  5. -- Other Files Required: 
  6. --     adobject.sx
  7. --
  8. -- Purpose: 
  9. --     Define the ClassifiedAds document and data objects
  10. --
  11. -- Specialized Classes:
  12. --     ClassifiedAds - adds the following instance variables
  13. --          adsPerPage:        an integer, the number of ads per page. This is 
  14. --              required to calculate the number of pages 
  15. --              based on the number of data objects
  16. --          adPageTemplate:    an instance of the built-in PageLayer class.  This is
  17. --              required when we create a new page.
  18. --          adProtocol:        an instance of whatever class is used to contain
  19. --              the data for the classified ads.  This is used to
  20. --              test if objects added to the classifieds are of the
  21. --              expected type.  It is also used as a "blank ad", to
  22. --              fill out pages that don't have a full complement of ads.
  23. --                     It also includes four methods:
  24. --          addClassified self adObject
  25. --                  Appends the adObject to the list of data objects.  If there are
  26. --                  not enough pages to display all of the data objects, a new page
  27. --                      is created.
  28. --          removeClassified self adObject
  29. --                  Removes the adObject from the list of data objects.  If the
  30. --                  removal makes the last page empty, that empty page is deleted.
  31. --          appendNewClassifiedPage self
  32. --                  (Private) Appends a new page to the document.
  33. --          pageData self #key pageNum
  34. --                  (Private) Returns the list of data objects which map to the
  35. --                  given pageNum.  Invoked (eventually) by the data functions of
  36. --                  the page elements in the adPageTemplate.
  37. -- Author:
  38. --     Steve Gano, Dionn M. Stewart, Felicia Santelli
  39.  
  40. in module Autofinder
  41.  
  42. class ClassifiedAds (Document)
  43. instance variables
  44.     adsPerPage            -- integer, number of ads per page
  45.     adPageTemplate        -- PageLayer object
  46.     adProtocol            -- single object of class acceptable for ad data
  47.     boundary            -- work around oneofnpresenter inheritance
  48.     pageData            -- data for that page
  49.     media
  50.     pageLeft            -- button for changing pages
  51.     pageRight            -- button for changing pages
  52.     control
  53. end
  54.  
  55. method init self {class ClassifiedAds} #rest args \
  56.                         #key media: (undefined) \
  57.                         adsPerPage:(3) \
  58.                         name:("Classified Ads") \
  59.                         boundary: (new rect) ->
  60. (
  61.     apply nextMethod self args
  62.  
  63.     -- We should test here for presence of required key args
  64.     self.adsPerPage := adsPerPage
  65.     self.boundary := boundary
  66.     self.pageData := #()
  67.     self.name := name
  68.     self.media := media
  69.     self
  70. )
  71.  
  72. method afterinit self {class ClassifiedAds} #rest args \
  73.             #key adPageTemplate:(undefined) adProtocol:(undefined) ->
  74. (
  75.     local media := self.media
  76.     
  77.     self.adPageTemplate := new ThreeAdPageTemplate \
  78.         boundary:self.boundary data:undefined media:media
  79.     self.adProtocol := new adDataObject
  80.     
  81.     --Make the pushbutton elements to turn the pages
  82.     self.pageLeft:= new pushButton pressedPresenter:media["left button"]\
  83.             releasedPresenter:(new twodshape boundary:media["left button"].bbox)\
  84.             disabledPresenter:media["left button"]
  85.     self.pageLeft.x := 13; self.pageLeft.y := 18
  86.     self.pageLeft.authordata := self
  87.     self.pageLeft.activateAction := (adata btn -> (updatePagebuttons adata @backward))
  88.      prepend self.adPageTemplate self.pageLeft
  89.  
  90.      self.pageRight:= new pushButton pressedpresenter:media["right button"]\
  91.          releasedPresenter:(new twodshape boundary:media["right button"].bbox)\
  92.          disabledpresenter:media["right button"]
  93.      self.pageRight.x := 577; self.pageRight.y := 18
  94.     self.pageRight.authordata := self
  95.      self.pageRight.activateAction:= (adata btn -> (updatePagebuttons adata @forward))
  96.      prepend self.adPageTemplate self.pageRight
  97.  
  98.      local ac:= new actuatorcontroller space:self.adPageTemplate
  99.      ac.wholespace:= true
  100.     self.control := #(ac)
  101.  
  102.     -- Create a blank page
  103.     appendNewClassifiedPage self
  104.  
  105.     self
  106. )
  107.  
  108. method leaveScene self {class ClassifiedAds} ->
  109. (
  110.     for i in self.control do
  111.         i.space := undefined
  112.     emptyout self.control
  113.  
  114.      for i in self.data do
  115.          makepurgeable i.adpic
  116.      
  117.     emptyOut self
  118.     )
  119.  
  120. method addClassified self {class ClassifiedAds} adObject ->
  121. (
  122.     -- Make sure the ad object is of the type we accept.
  123.     if (getclass adObject) <> (getClass self.adProtocol) do return
  124.     
  125.     -- If the ad is already in our list, just return
  126.     if (ismember self.data adObject) do return
  127.     
  128.     append self.data adObject
  129.     
  130.     -- If all of the pages are full, add a new page for this object.
  131.     if trunc (((size self.data - 1)/ self.adsPerPage) + 1) > size self do
  132.     (
  133.         appendNewClassifiedPage self
  134.     )
  135. )
  136.  
  137. method removeClassified self {class ClassifiedAds} adObject ->
  138. (
  139.     -- If the ad object is not currently on our list, just return
  140.     if not (ismember self.data adObject) do return
  141.  
  142.     -- Delete the object from the data list.    
  143.     deleteOne self.data adObject
  144.     
  145.     -- If this made an empty page, delete it (unless it's the only page)
  146.     if ((((size self.data - 1)/ self.adsPerPage) + 1) < size self) and
  147.             (size self > 1) do
  148.     (
  149.         deleteOne self (getnth self (size self))
  150.     )
  151. )
  152.  
  153. method appendNewClassifiedPage self {class ClassifiedAds} ->
  154. (
  155.     -- Append a new page, using the adPageTemplate page layer.
  156.     -- Use our pageData method as the way to derive the data for the page.
  157.     append self (new page boundary:self.boundary frame:self.adPageTemplate \
  158.                             data:(p -> pageData p.presentedBy) )
  159. )
  160.  
  161. method pageData self {class ClassifiedAds} #key pageNum:(self.cursor) ->
  162. (
  163.     -- Return a list of the ad data objects for the given page.
  164.     local firstObjNdx := max 1 (((pageNum - 1) * self.adsPerPage) + 1)
  165.     local lastObjNdx := min (firstObjNdx + 2) (size self.data)
  166.  
  167.     -- In some cases (e.g., no ads), firstNdx will be greater than lastNdx
  168.     local ads :=
  169.     if firstObjNdx > lastObjNdx then
  170.         #()
  171.     else
  172.         for objNdx := firstObjNdx to lastObjNdx collect 
  173.                                                     (getnth self.data objNdx)
  174.  
  175.     -- If the page is not full, fill it out with blank ads.
  176.     repeat while (size ads < self.adsPerPage) do append ads self.adProtocol
  177.  
  178.     ads
  179. )
  180.  
  181. --Each time you change the page you want to make the pics purgeable
  182. method changePage self {class ClassifiedAds} page ->
  183. (
  184.     local previousPageData, nextPageData
  185.  
  186.     previousPageData := self.pageData
  187.     self.pageData := pageData self pageNum:self.cursor
  188.  
  189.     -- To be sure page updating occurs all at once
  190.     if (self.presentedBy !== undefined) do
  191.         self.presentedBy.compositor.enabled := false
  192.     nextmethod self page
  193.     if (self.presentedBy !== undefined) do
  194.         self.presentedBy.compositor.enabled := true
  195.     
  196.      foreach previousPageData (item arg -> \
  197.          if item <> arg.adProtocol do makepurgeable item.adpic) self
  198. )
  199.  
  200. method updatePageButtons self {class ClassifiedAds} direction ->
  201. (
  202.     -- check to make sure we don't page out of bounds
  203.     if direction = @backward then
  204.         self.cursor:= max 1 (self.cursor - 1)
  205.     else
  206.         self.cursor:= min (size self) (self.cursor + 1)
  207.  
  208.     -- if we are on the first page, disable the pageLeft button
  209.     self.pageLeft.enabled := self.cursor > 1
  210.  
  211.     -- if we are on the last page, disable the pageRight button
  212.     self.pageRight.enabled := self.cursor < (size self)
  213.     undefined
  214. )
  215.  
  216. -- Create a couple of convenience functions which take a Selection (the kind
  217. -- of result returned by a selection statement), and add or remove the ad objects
  218. -- to/from the classified ads.
  219. method postAds  self {class ClassifiedAds} adList ->
  220. (
  221.     for c in adList do addClassified self c
  222.  
  223.     -- Goto the first page
  224.     goto self self.cursor
  225.     forward self
  226. )
  227.  
  228. method unPostAds  self {class ClassifiedAds} adList ->
  229. (
  230.     for c in adList do removeClassified self c
  231.     goto self self.cursor
  232. )
  233.  
  234. "Compiled ads.sx"
  235. -->>>
  236.